<template>
   <van-nav-bar title="文件下载" />
  <div>
     <van-button type="primary" style=" margin-top: 20px;" @click="downloadFile" :loading="downloading">下载文件</van-button>
  </div>
  <div>
    <van-field v-model="testUrl" label="下载地址" placeholder="" />
   <div>{{result  }}</div>
  </div>
</template>

<script setup lang='ts' name='' >
// Your script here

import { ref } from "vue";

let testUrl =  ref("http://192.168.1.2:10086/new.apk");
let result = ref("")
let downloading = ref(false);

const downloadFile= ()=> {
  let passObj = {
    "url":  encodeURIComponent(testUrl.value)
  };
  downloading.value = true;
  (window as any).whJSBridge.downloadFile(JSON.stringify(passObj)).then(
    function(res:any) {
      console.log(res);
      result.value = "文件位置："+res;
      downloading.value = false;
    }).catch(function(err:any) {
      downloading.value = false;
    if ('object' == typeof err) {
      alert('下载文件失败：' + JSON.stringify(err))
    } else {
      alert('下载文件失败：' + err)
    }
  });

}
</script>

<style scoped >
/* Your styles here */
</style>